import { request } from '../utils/request';
import type { ApiResponse } from '../types/common';

export interface LoginParams {
  username: string;
  password: string;
}

export interface RegisterParams {
  username: string;
  password: string;
  email: string;
  phone: string;
}

export interface UserInfo {
  userId: string;
  username: string;
  role: string;
  lastLogin: string;
}

export interface LoginResponse {
  token: string;
  user: {
    username: string;
    identity: "enterprise" | "research" | "government"; // 企业（创新版）、院校（科研）、政府（决策）
    email: string;
    phone: string;
    identity_display: string;
    is_superuser: boolean; // 是否是超级管理员
    created_at: string;
  }
}

export const authApi = {
  // 用户登录
  login(data: LoginParams) {
    return request.post<any, ApiResponse<LoginResponse>>(
      '/users/login/',
      data
    );
  },

  // 用户注册
  register(data: RegisterParams) {
    return request.post<any, ApiResponse<LoginResponse>>(
      '/users/register/',
      data,
    );
  },

  // 用户注销
  logout() {
    return request.post<any, ApiResponse<null>>(
      '/users/logout/',
    );
  },

  // 获取用户信息
  getUserInfo() {
    return request.get<any, ApiResponse<UserInfo>>(
      '/users/info/',
    );
  },

  // // 修改密码
  // changePassword(data: { oldPassword: string; newPassword: string }) {
  //   return request.put<any, ApiResponse<null>>(
  //     '/user/password/',
  //     data,
  //   );
  // },

  // // 修改邮箱
  // changeEmail(data: { email: string }) {
  //   return request.put<any, ApiResponse<null>>(
  //     '/user/email/',
  //     data,
  //   );
  // },

  // // 忘记密码
  // forgotPassword(email: string) {
  //   return request.post<any, ApiResponse<null>>(
  //     '/user/forgot-password/',
  //     { email },
  //   );
  // }
};